/**
 * Created by Brant on 2016/12/28.
 */
module.exports = function ($http,httpservice,$q) {
    var caseBarDetailpeopleService = {};


    /*获取当事人列表*/
    caseBarDetailpeopleService.getDSRList = function (caseId) {
        var params={"caseId":caseId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getCaseClientsUrl,
            params:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*获取对方当事人列表*/
    caseBarDetailpeopleService.getDFDSRList = function (caseId,caseClientType) {
        var params={"caseId":caseId,"caseClientType":caseClientType};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getDFDSRUrl,
            params:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*获取审判人列表*/
    caseBarDetailpeopleService.getSPRList = function (caseId) {
        var params={"caseId":caseId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getSPRUrl,
            params:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*获取第三人列表*/
    caseBarDetailpeopleService.getDSANRList = function (caseId) {
        var params={"caseId":caseId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getDSANRUrl,
            params:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

  /**
   * 当事人，对方当事人，审判人员，第三人
   * @param caseId
   * @returns {Function}
   */
    caseBarDetailpeopleService.getTotalMemberLists = function (caseId) {
      var params={"caseId":caseId};
      var deferred = $q.defer();
      $http({
        method: 'GET',
        url: httpservice.getTotalMemberListsUrl,
        params:params,
        headers : {'Content-Type':'application/json'}
      }).then(function successCallback(response) {
        deferred.resolve(response.data);
      }, function errorCallback(response) {
        deferred.reject(response);
      });
      return deferred.promise;
    };





    //添加
    /*添加当事人*/
    caseBarDetailpeopleService.addDSR = function (caseId,caseType,caseClientType,AdversaryClientName,clientId,userId) {
        var params={"caseId":caseId,"caseType":caseType,"caseClientType":caseClientType,"caseClientName":AdversaryClientName,"clientId":clientId,"userId":userId};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addDSRUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*添加对方当事人*/
    caseBarDetailpeopleService.addDFDSR = function (caseId,caseType,caseClientType,AdversaryClientName,userId) {
        var params={"caseId":caseId,"caseType":caseType,"caseClientType":caseClientType,"adversaryClientName":AdversaryClientName,"userId":userId};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addDFDSRUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*添加审判人*/
    caseBarDetailpeopleService.addSPR = function (caseId,judgeRole,judgeName,userId) {
        var params={"caseId":caseId,"judgeRole":judgeRole,"judgeName":judgeName,"userId":userId};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addSPRUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /*添加第三人*/
    caseBarDetailpeopleService.addDSANR = function (caseId,thirdPartyName,userId) {
        var params={"caseId":caseId,"thirdPartyName":thirdPartyName,"userId":userId};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addDSANRUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };




    //删除当事人
    caseBarDetailpeopleService.deleteDSR = function (id,caseId,caseClientName,userId,caseName) {
        var params={"id":id,"caseId":caseId,"caseClientName":caseClientName,"userId":userId,"caseName":caseName};
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteDSRUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };
    //删除对方当事人
    caseBarDetailpeopleService.deleteDFDSR = function (id,caseId,adversaryClientName,userId) {
        var params={"id":id,"caseId":caseId,"adversaryClientName":adversaryClientName,"userId":userId};
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteDFDSRUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //删除审判人
    caseBarDetailpeopleService.deleteSPR = function (id,caseId,judgeName,userId) {
        var params={"id":id,"caseId":caseId,"judgeName":judgeName,"userId":userId};
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteSPRUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //删除第三人
    caseBarDetailpeopleService.deleteDSANR = function (id,caseId,thirdPartyName,userId) {
        var params={"id":id,"caseId":caseId,"thirdPartyName":thirdPartyName,"userId":userId};
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteDSANRUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };




    ////修改当事人
    //caseBarDetailpeopleService.updateDSR = function (id,caseClientName) {
    //    var params={"id":id,"caseClientName":caseClientName};
    //    var deferred = $q.defer();
    //    $http({
    //        method: 'PUT',
    //        url: httpservice.updateDSRUrl,
    //        data:params,
    //        headers : {'Content-Type':'application/json'}
    //    }).then(function successCallback(response) {
    //        deferred.resolve(response.data);
    //    }, function errorCallback(response) {
    //        deferred.reject(response);
    //    });
    //    return deferred.promise;
    //};
    //
    ////修改对方当事人
    //caseBarDetailpeopleService.updateDFDSR = function (id,adversaryClientName) {
    //    var params={"id":id,"adversaryClientName":adversaryClientName};
    //    var deferred = $q.defer();
    //    $http({
    //        method: 'PUT',
    //        url: httpservice.updateDFDSRUrl,
    //        data:params,
    //        headers : {'Content-Type':'application/json'}
    //    }).then(function successCallback(response) {
    //        deferred.resolve(response.data);
    //    }, function errorCallback(response) {
    //        deferred.reject(response);
    //    });
    //    return deferred.promise;
    //};

    /*查询自然人列表的接口*/
    caseBarDetailpeopleService.selectZiRanRenInfo = function (biglawuserid, pageSize, page, name,caseId) {
        var deferred = $q.defer();
        var params = {'pageNum': page, 'pageSize': pageSize, 'biglawuserid': biglawuserid, 'name':name,'caseId':caseId};
        $http({
            method: 'GET',
            url: httpservice.selectZRRCustomerUrl,
            params: params,
            dataType: 'json'
            //headers: {'Content-Type': 'application/json'}

        }).then(function successCallback(response) {
            //当前页
            var maxPage = page;
            if (maxPage === "") {
                maxPage = 1;
            }
            //总页数
            var total = response.data.totalPage;
            var startPage = response.data.beginPage;
            var endPage = response.data.endPage;
            var totalCount = response.data.totalCount;
            if (total <= maxPage) {
                maxPage = total;
            }
            if (maxPage <= 0) {
                maxPage = 1;
            }
            var totalPas = {};
            for (var i = startPage; i <= endPage; i++) {
                totalPas[i - 1] = i;
            }
            //设置当前页和总页数
            response.data.totalPage = total;
            // response.data.nowPage = maxPage + "/" + total;
            response.data.totalPas = totalPas;
            response.data.totalCount = totalCount;

            // response.data.page = maxPage;
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response.data);
        });
        return deferred.promise;
    };
    /*查询法人列表的接口*/
    caseBarDetailpeopleService.selectFaRenInfo = function (loginId, pageSize, page, companyname,caseId) {
        var deferred = $q.defer();
        var params = {'pageNum': page, 'pageSize': pageSize, 'biglawuserid': loginId, 'companyname':companyname,'caseId':caseId};
        $http({
            method: 'GET',
            url: httpservice.selectFRCustomerUrl,
            params: params,
            dataType: 'json'
            //headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            //当前页
            var maxPage = page;
            if (maxPage === "") {
                maxPage = 1;
            }
            //总页数
            var total = response.data.totalPage;
            var startPage = response.data.beginPage;
            var endPage = response.data.endPage;
            var totalCount = response.data.totalCount;
            if (total <= maxPage) {
                maxPage = total;
            }
            if (maxPage <= 0) {
                maxPage = 1;
            }
            var totalPas = {};
            for (var i = startPage; i <= endPage; i++) {
                totalPas[i - 1] = i;
            }
            //设置当前页和总页数
            response.data.totalPage = total;
            // response.data.nowPage = maxPage + "/" + total;
            response.data.totalPas = totalPas;
            response.data.totalCount = totalCount;
            response.data.page = maxPage;
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response.data);
        });
        return deferred.promise;
    };

    //添加自然人
    caseBarDetailpeopleService.addZRRCustomer = function (loginId, name, sex, phone, birthday, nationality, profession, workplace, address, status, remark) {
        var params = {
            "biglawuserid": loginId,
            "name": name,
            "sex": sex,
            "contactnumber": phone,
            "birthday": birthday,
            "nationality": nationality,
            "profession": profession,
            "workplace": workplace,
            "address": address,
            "status": status,
            "remark": remark
        };
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addZRRCustomerUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //添加法人
    caseBarDetailpeopleService.addFRCustomer = function (loginId, companyname, address, representative, post, contactname, contactphone,
                                                 workplace, status, remark,customerGroupId) {

        var params = {
            "biglawuserid": loginId,
            "companyname": companyname,
            "address": address,
            "representative": representative,
            "post": post,
            "contactname": contactname,
            "contactphone": contactphone,
            "workplace": workplace,
            "status": status,
            "remark": remark,
            "customerGroupId": customerGroupId,
        };
        //var deferred = $q.defer();
        //$http({
        //    method: 'POST',
        //    url: httpservice.addFRCustomerUrl,
        //    data:params,
        //    headers : {'Content-Type':'application/json'}
        //}).then(function successCallback(response) {
        //    deferred.resolve(response.data);
        //}, function errorCallback(response) {
        //    deferred.reject(response);
        //});
        //return deferred.promise;
        return $http(
            {
                url: httpservice.addFRCustomerUrl,
                method: 'POST',
                params: params,
                dataType: 'json',
                headers: {'Content-Type': 'application/json'}
            }
        )
            .then(function (response) {
                return response.data;
            })
            .catch(function (response) {
                console.error('login error', response.status, response.data);
                $("#errormsgId").html("登录失败，失败原因：" + response.status);
            })
            .finally(function () {
                /*$("#errormsgId").html("网络链接失败！");*/
            });
    };


    /**
     * 修改当事人名字
     * @param userId 用户ID
     * @param caseId 案件ID
     * @param relatedStatus 状态：1-当事人，2-对方当事人，3-审判人员，4-第三人
     * @param relatedName 修改后的名字
     * @param relatedId 相关人员ID
     * @returns {Promise}
     */
    caseBarDetailpeopleService.updateCaseRelatedPersonName = function (userId, caseId, relatedStatus, relatedName, relatedId,casename) {
        var params = {
            "userId": userId,
            "caseId": caseId,
            "relatedStatus": relatedStatus,
            "relatedName": relatedName,
            "relatedId": relatedId,
            "caseName": casename
        };
        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.updateCaseRelatedPersonNameUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };



    return caseBarDetailpeopleService;
}
